//
//  ZLMainView.swift
//  BasicProduct
//
//  Created by itzhaolei on 2021/8/31.
//

import Foundation
import MJRefresh

open class ZLMainView: UIView {
    
    // 启用
    public var scrollViewEnabled: Bool = false
    public var tableViewEnabled: Bool = false
    public var collectionEnabled: Bool = false
    
    public lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        //配置
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
            scrollView.scrollIndicatorInsets = scrollView.contentInset
        }
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
        return scrollView
    }()
    
    public lazy var tableView: UITableView = {
        let tableView = UITableView()
        //配置
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.scrollIndicatorInsets = tableView.contentInset
            tableView.estimatedRowHeight = 0
            tableView.estimatedSectionHeaderHeight = 0
            tableView.estimatedSectionFooterHeight = 0
        }
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.alwaysBounceVertical = false
        tableView.alwaysBounceHorizontal = false
        tableView.sectionHeaderHeight = 0
        tableView.sectionFooterHeight = 0
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0.01))
        tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0.01))
        return tableView
    }()
    
    public lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView()
        //配置
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
            collectionView.scrollIndicatorInsets = collectionView.contentInset
        }
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.alwaysBounceVertical = false
        collectionView.alwaysBounceHorizontal = false
        return collectionView
    }()
    
    /// 添加子视图
    open func addSubviews() {
        
    }
    
    /// 添加约束
    open func addConstraints() {
        
    }
    
    /// 添加事件
    open func addActions() {
        
    }
    
    /// 结束刷新，并修复滑动视图的显示状态
    /// - Parameter noMore: 没有更多数据
    open func endRefreshFixState(noMore: Bool) {
        if scrollViewEnabled {
           
        }
        if tableViewEnabled {
            tableView.reloadData()
            tableView.mj_header?.endRefreshing()
            tableView.mj_footer?.endRefreshing()
            
            noMore ? tableView.mj_footer?.endRefreshingWithNoMoreData() : tableView.mj_footer?.resetNoMoreData()
        }
        if collectionEnabled {
            tableView.reloadData()
            collectionView.mj_header?.endRefreshing()
            collectionView.mj_footer?.endRefreshing()
            
            noMore ? collectionView.mj_footer?.endRefreshingWithNoMoreData() : collectionView.mj_footer?.resetNoMoreData()
        }
    }
    
}
